<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="ssl-objects.html" />
<link rel="prev" href="module-socket.html" />
<link rel="parent" href="module-socket.html" />
<link rel="next" href="ssl-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>17.2.1 Socket Objects </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="17.2 socket  "
  href="module-socket.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="17.2 socket  "
  href="module-socket.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="17.2.2 ssl Objects"
  href="ssl-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-socket.html">17.2 socket  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-socket.html">17.2 socket  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="ssl-objects.html">17.2.2 SSL Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0019210000000000000000"></a><a name="socket-objects"></a>
<br>
17.2.1 Socket Objects 
</h2>

<p>
Socket objects have the following methods.  Except for
<tt class="method">makefile()</tt> these correspond to <span class="Unix">Unix</span> system calls
applicable to sockets.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3693' xml:id='l2h-3693' class="method">accept</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Accept a connection.
The socket must be bound to an address and listening for connections.
The return value is a pair <code>(<var>conn</var>, <var>address</var>)</code>
where <var>conn</var> is a <em>new</em> socket object usable to send and
receive data on the connection, and <var>address</var> is the address bound
to the socket on the other end of the connection.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3694' xml:id='l2h-3694' class="method">bind</tt></b>(</nobr></td>
  <td><var>address</var>)</td></tr></table></dt>
<dd>
Bind the socket to <var>address</var>.  The socket must not already be bound.
(The format of <var>address</var> depends on the address family -- see
above.)  <span class="note"><b class="label">Note:</b>
This method has historically accepted a pair
of parameters for <tt class="constant">AF_INET</tt> addresses instead of only a
tuple.  This was never intentional and is no longer available in
Python 2.0 and later.</span>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3695' xml:id='l2h-3695' class="method">close</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Close the socket.  All future operations on the socket object will fail.
The remote end will receive no more data (after queued data is flushed).
Sockets are automatically closed when they are garbage-collected.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3696' xml:id='l2h-3696' class="method">connect</tt></b>(</nobr></td>
  <td><var>address</var>)</td></tr></table></dt>
<dd>
Connect to a remote socket at <var>address</var>.
(The format of <var>address</var> depends on the address family -- see
above.)  <span class="note"><b class="label">Note:</b>
This method has historically accepted a pair
of parameters for <tt class="constant">AF_INET</tt> addresses instead of only a
tuple.  This was never intentional and is no longer available in
Python 2.0 and later.</span>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3697' xml:id='l2h-3697' class="method">connect_ex</tt></b>(</nobr></td>
  <td><var>address</var>)</td></tr></table></dt>
<dd>
Like <code>connect(<var>address</var>)</code>, but return an error indicator
instead of raising an exception for errors returned by the C-level
<tt class="cfunction">connect()</tt> call (other problems, such as ``host not found,''
can still raise exceptions).  The error indicator is <code>0</code> if the
operation succeeded, otherwise the value of the <tt class="cdata">errno</tt>
variable.  This is useful to support, for example, asynchronous connects.
<span class="note"><b class="label">Note:</b>
This method has historically accepted a pair of
parameters for <tt class="constant">AF_INET</tt> addresses instead of only a tuple.
This was never intentional and is no longer available in Python
2.0 and later.</span>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3698' xml:id='l2h-3698' class="method">fileno</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the socket's file descriptor (a small integer).  This is useful
with <tt class="function">select.select()</tt>.

<p>
Under Windows the small integer returned by this method cannot be used where
a file descriptor can be used (such as <tt class="function">os.fdopen()</tt>).  <span class="Unix">Unix</span> does
not have this limitation.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3699' xml:id='l2h-3699' class="method">getpeername</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the remote address to which the socket is connected.  This is
useful to find out the port number of a remote IPv4/v6 socket, for instance.
(The format of the address returned depends on the address family --
see above.)  On some systems this function is not supported.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3700' xml:id='l2h-3700' class="method">getsockname</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the socket's own address.  This is useful to find out the port
number of an IPv4/v6 socket, for instance.
(The format of the address returned depends on the address family --
see above.)
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3701' xml:id='l2h-3701' class="method">getsockopt</tt></b>(</nobr></td>
  <td><var>level, optname</var><big>[</big><var>, buflen</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return the value of the given socket option (see the <span class="Unix">Unix</span> man page
<span class="manpage"><i>getsockopt</i>(2)</span>).  The needed symbolic constants
(<tt class="constant">SO_*</tt> etc.) are defined in this module.  If <var>buflen</var>
is absent, an integer option is assumed and its integer value
is returned by the function.  If <var>buflen</var> is present, it specifies
the maximum length of the buffer used to receive the option in, and
this buffer is returned as a string.  It is up to the caller to decode
the contents of the buffer (see the optional built-in module
<tt class="module"><a href="module-struct.html">struct</a></tt> for a way to decode C structures encoded as strings).
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3702' xml:id='l2h-3702' class="method">listen</tt></b>(</nobr></td>
  <td><var>backlog</var>)</td></tr></table></dt>
<dd>
Listen for connections made to the socket.  The <var>backlog</var> argument
specifies the maximum number of queued connections and should be at
least 1; the maximum value is system-dependent (usually 5).
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3703' xml:id='l2h-3703' class="method">makefile</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a <i class="dfn">file object</i> associated with the socket.  (File objects
are described in <a href="bltin-file-objects.html#bltin-file-objects">3.9</a>, ``File Objects.'')
The file object references a <tt class="cfunction">dup()</tt>ped version of the
socket file descriptor, so the file object and socket object may be
closed or garbage-collected independently.
The socket must be in blocking mode.
<a id='l2h-3719' xml:id='l2h-3719'></a>The optional <var>mode</var>
and <var>bufsize</var> arguments are interpreted the same way as by the
built-in <tt class="function">file()</tt> function; see ``Built-in Functions''
(section <a href="built-in-funcs.html#built-in-funcs">2.1</a>) for more information.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3704' xml:id='l2h-3704' class="method">recv</tt></b>(</nobr></td>
  <td><var>bufsize</var><big>[</big><var>, flags</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Receive data from the socket.  The return value is a string representing
the data received.  The maximum amount of data to be received
at once is specified by <var>bufsize</var>.  See the <span class="Unix">Unix</span> manual page
<span class="manpage"><i>recv</i>(2)</span> for the meaning of the optional argument
<var>flags</var>; it defaults to zero.
<span class="note"><b class="label">Note:</b>
For best match with hardware and network realities, the value of 
<var>bufsize</var> should be a relatively small power of 2, for example, 4096.</span>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3705' xml:id='l2h-3705' class="method">recvfrom</tt></b>(</nobr></td>
  <td><var>bufsize</var><big>[</big><var>, flags</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Receive data from the socket.  The return value is a pair
<code>(<var>string</var>, <var>address</var>)</code> where <var>string</var> is a string
representing the data received and <var>address</var> is the address of the
socket sending the data.  The optional <var>flags</var> argument has the
same meaning as for <tt class="method">recv()</tt> above.
(The format of <var>address</var> depends on the address family -- see above.)
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3706' xml:id='l2h-3706' class="method">recvfrom_into</tt></b>(</nobr></td>
  <td><var>buffer</var><big>[</big><var>, nbytes</var><big>[</big><var>, flags</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Receive data from the socket, writing it into <var>buffer</var> instead of 
creating a new string.  The return value is a pair
<code>(<var>nbytes</var>, <var>address</var>)</code> where <var>nbytes</var> is the number
of bytes received and <var>address</var> is the address of the socket
sending the data.  See the <span class="Unix">Unix</span> manual page
<span class="manpage"><i>recv</i>(2)</span> for the meaning of the optional argument
<var>flags</var>; it defaults to zero.  (The format of <var>address</var>
depends on the address family -- see above.)

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3707' xml:id='l2h-3707' class="method">recv_into</tt></b>(</nobr></td>
  <td><var>buffer</var><big>[</big><var>, nbytes</var><big>[</big><var>, flags</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Receive up to <var>nbytes</var> bytes from the socket,
storing the data into a buffer rather than creating a new string.    
If <var>nbytes</var> is not specified (or 0), 
receive up to the size available in the given buffer.
See the <span class="Unix">Unix</span> manual page <span class="manpage"><i>recv</i>(2)</span> for the meaning of the
optional argument <var>flags</var>; it defaults to zero.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3708' xml:id='l2h-3708' class="method">send</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, flags</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Send data to the socket.  The socket must be connected to a remote
socket.  The optional <var>flags</var> argument has the same meaning as for
<tt class="method">recv()</tt> above.  Returns the number of bytes sent.
Applications are responsible for checking that all data has been sent;
if only some of the data was transmitted, the application needs to
attempt delivery of the remaining data.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3709' xml:id='l2h-3709' class="method">sendall</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, flags</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Send data to the socket.  The socket must be connected to a remote
socket.  The optional <var>flags</var> argument has the same meaning as for
<tt class="method">recv()</tt> above.  Unlike <tt class="method">send()</tt>, this method continues
to send data from <var>string</var> until either all data has been sent or
an error occurs.  <code>None</code> is returned on success.  On error, an
exception is raised, and there is no way to determine how much data,
if any, was successfully sent.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3710' xml:id='l2h-3710' class="method">sendto</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, flags</var><big>]</big><var>, address</var>)</td></tr></table></dt>
<dd>
Send data to the socket.  The socket should not be connected to a
remote socket, since the destination socket is specified by
<var>address</var>.  The optional <var>flags</var> argument has the same
meaning as for <tt class="method">recv()</tt> above.  Return the number of bytes sent.
(The format of <var>address</var> depends on the address family -- see above.)
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3711' xml:id='l2h-3711' class="method">setblocking</tt></b>(</nobr></td>
  <td><var>flag</var>)</td></tr></table></dt>
<dd>
Set blocking or non-blocking mode of the socket: if <var>flag</var> is 0,
the socket is set to non-blocking, else to blocking mode.  Initially
all sockets are in blocking mode.  In non-blocking mode, if a
<tt class="method">recv()</tt> call doesn't find any data, or if a
<tt class="method">send()</tt> call can't immediately dispose of the data, a
<tt class="exception">error</tt> exception is raised; in blocking mode, the calls
block until they can proceed.
<code>s.setblocking(0)</code> is equivalent to <code>s.settimeout(0)</code>;
<code>s.setblocking(1)</code> is equivalent to <code>s.settimeout(None)</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3712' xml:id='l2h-3712' class="method">settimeout</tt></b>(</nobr></td>
  <td><var>value</var>)</td></tr></table></dt>
<dd>
Set a timeout on blocking socket operations.  The <var>value</var> argument
can be a nonnegative float expressing seconds, or <code>None</code>.
If a float is
given, subsequent socket operations will raise an <tt class="exception">timeout</tt>
exception if the timeout period <var>value</var> has elapsed before the
operation has completed.  Setting a timeout of <code>None</code> disables
timeouts on socket operations.
<code>s.settimeout(0.0)</code> is equivalent to <code>s.setblocking(0)</code>;
<code>s.settimeout(None)</code> is equivalent to <code>s.setblocking(1)</code>.

<span class="versionnote">New in version 2.3.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3713' xml:id='l2h-3713' class="method">gettimeout</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the timeout in floating seconds associated with socket
operations, or <code>None</code> if no timeout is set.  This reflects
the last call to <tt class="method">setblocking()</tt> or <tt class="method">settimeout()</tt>.

<span class="versionnote">New in version 2.3.</span>

</dl>

<p>
Some notes on socket blocking and timeouts: A socket object can be in
one of three modes: blocking, non-blocking, or timeout.  Sockets are
always created in blocking mode.  In blocking mode, operations block
until complete.  In non-blocking mode, operations fail (with an error
that is unfortunately system-dependent) if they cannot be completed
immediately.  In timeout mode, operations fail if they cannot be
completed within the timeout specified for the socket.  The
<tt class="method">setblocking()</tt> method is simply a shorthand for certain
<tt class="method">settimeout()</tt> calls.

<p>
Timeout mode internally sets the socket in non-blocking mode.  The
blocking and timeout modes are shared between file descriptors and
socket objects that refer to the same network endpoint.  A consequence
of this is that file objects returned by the <tt class="method">makefile()</tt>
method must only be used when the socket is in blocking mode; in
timeout or non-blocking mode file operations that cannot be completed
immediately will fail.

<p>
Note that the <tt class="method">connect()</tt> operation is subject to the timeout
setting, and in general it is recommended to call
<tt class="method">settimeout()</tt> before calling <tt class="method">connect()</tt>.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3714' xml:id='l2h-3714' class="method">setsockopt</tt></b>(</nobr></td>
  <td><var>level, optname, value</var>)</td></tr></table></dt>
<dd>
Set the value of the given socket option (see the <span class="Unix">Unix</span> manual page
<span class="manpage"><i>setsockopt</i>(2)</span>).  The needed symbolic constants are defined in
the <tt class="module">socket</tt> module (<tt class="constant">SO_*</tt> etc.).  The value can be an
integer or a string representing a buffer.  In the latter case it is
up to the caller to ensure that the string contains the proper bits
(see the optional built-in module
<tt class="module"><a href="module-struct.html">struct</a></tt><a id='l2h-3720' xml:id='l2h-3720'></a> for a way to encode C
structures as strings). 
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3715' xml:id='l2h-3715' class="method">shutdown</tt></b>(</nobr></td>
  <td><var>how</var>)</td></tr></table></dt>
<dd>
Shut down one or both halves of the connection.  If <var>how</var> is
<tt class="constant">SHUT_RD</tt>, further receives are disallowed.  If <var>how</var> is <tt class="constant">SHUT_WR</tt>,
further sends are disallowed.  If <var>how</var> is <tt class="constant">SHUT_RDWR</tt>, further sends
and receives are disallowed.
</dl>

<p>
Note that there are no methods <tt class="method">read()</tt> or <tt class="method">write()</tt>;
use <tt class="method">recv()</tt> and <tt class="method">send()</tt> without <var>flags</var> argument
instead.

<p>
Socket objects also have these (read-only) attributes that correspond
to the values given to the <tt class="class">socket</tt> constructor.

<p>
<dl><dt><b><tt id='l2h-3716' xml:id='l2h-3716' class="member">family</tt></b></dt>
<dd>
The socket family.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
<dl><dt><b><tt id='l2h-3717' xml:id='l2h-3717' class="member">type</tt></b></dt>
<dd>
The socket type.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
<dl><dt><b><tt id='l2h-3718' xml:id='l2h-3718' class="member">proto</tt></b></dt>
<dd>
The socket protocol.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="17.2 socket  "
  href="module-socket.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="17.2 socket  "
  href="module-socket.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="17.2.2 ssl Objects"
  href="ssl-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-socket.html">17.2 socket  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-socket.html">17.2 socket  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="ssl-objects.html">17.2.2 SSL Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
